<%--
  @author Supun Viraj Rathnayaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script src="${pageContext.request.contextPath}/js/printer.js" type="text/javascript"></script>
<%--<link rel="stylesheet" href="${pageContext.request.contextPath}/css/printDetailForm.css" media="print"/>--%>

<script type="text/javascript">
var page = 1;
var previousPage = 0;
var resetPage = true;

$(function () {
    initHeader();
    showPrevButton(false);
    showNextButton(false);
});

function initHeader() {
    var opt = '';
    opt += '<thead>' +
            '<tr class="table-header">' +
            '<th class="center-align">' + $('#ref').val() + '</th>' +
            '<th class="center-align">' + $('#dsDiv').val() + '</th>' +
            '<th class="left-align">' + $('#gnDiv').val() + '</th>' +
            '<th class="center-align">' + $('#lname').val() + '</th>' +
            '<th class="center-align">' + $('#plan').val() + '</th>' +
            '<th class="center-align">' + $('#lot').val() + '</th>' +
            '<th class="center-align">' + $('#ext_hec').val() + '</th>' +
            '</tr>' +
            '</thead>';
    $('.land-table').html(opt);
    setDataTable();
}

function setDataTable() {
    $('.land-table').dataTable({
        "sPaginationType": "full_numbers",
        "bDestroy": true,
        "oLanguage": {
            "sZeroRecords": $('#no-record').val(),
            "sLengthMenu": $('#display').val() + " _MENU_ " + $('#records').val(),
            "sSearch": $('#search').val(),
            "sInfo": $('#showing').val() + " _START_ " + $('#to').val() + " _END_ " + $('#of').val() + " _TOTAL_ " + $('#entries').val(),
            "sInfoEmpty": $('#no-record').val(),
            "oPaginate": {
                "sFirst": $('#first').val(),
                "sLast": $('#last').val(),
                "sNext": $('#next').val(),
                "sPrevious": $('#previous').val()
            }
        },
        "fnDrawCallback": function () {
            if ($('.dataTables_paginate span span.paginate_button').size()) {
                $('.dataTables_paginate')[0].style.display = "block";
            } else {
                $('.dataTables_paginate')[0].style.display = "none";
            }
        },
        "aLengthMenu": [
            [ 10, 25, 50],
            [10, 25, 50]
        ],
        "iDisplayLength": 50
    });
}

function loadLandList(landInfo) {
    var opt = '';
    opt += '<thead>' +
            '<tr class="table-header">' +
            '<th class="center-align">' + $('#ref').val() + '</th>' +
            '<th class="center-align">' + $('#dsDiv').val() + '</th>' +
            '<th class="left-align">' + $('#gnDiv').val() + '</th>' +
            '<th class="center-align">' + $('#lname').val() + '</th>' +
            '<th class="center-align">' + $('#plan').val() + '</th>' +
            '<th class="center-align">' + $('#lot').val() + '</th>' +
            '<th class="center-align">' + $('#ext_hec').val() + '</th>' +
            '</tr>' +
            '</thead>';
    opt += '<tbody>';

    if (landInfo.length > 0) {
        for (var i = 0; i < landInfo.length; i++) {
            var reference = (landInfo[i].referenceNumber > 0) ? landInfo[i].referenceNumber : landInfo[i].id;
            var extent = (landInfo[i].kmlLandArea > 0) ? landInfo[i].kmlLandArea : landInfo[i].landExtent;
            //extent=roundNumber(extent, 4);
            extent = getHect(extent);
            var scheduleDate = (landInfo[i].scheduleDate != null) ? landInfo[i].scheduleDate : '';
            var alienationNumber = (landInfo[i].alienationNumber != 0) ? landInfo[i].alienationNumber : '';
            opt += '<tr>' +
                    '<td class="center-align">' + reference + '</td>' +
                    '<td class="center-align">' + landInfo[i].dsDivision + '</td>' +
                    '<td class="center-align">' + landInfo[i].gnDivision + '</td>' +
                    '<td class="center-align">' + landInfo[i].nameOfTheLand + '</td>' +
                    '<td class="center-align">' + landInfo[i].planNumber + '</td>' +
                    '<td class="center-align">' + landInfo[i].lotNumber + '</td>' +
                    '<td class="center-align">' + extent + '</td>' +
                    '</tr>';
        }
    }
    opt += '</tbody>';
    $('.land-table').html(opt);
    setDataTable();

}

$(document).ready(function () {
    $('.print-btn').hide();
    $('#report-header').hide();
    $('.print-btn').printPreview({

    });
});

function roundNumber(number, decimals) { // Arguments: number to round, number of decimal places
    var newnumber = new Number(number + '').toFixed(parseInt(decimals));
    return parseFloat(newnumber);
}

function getHect(pershes) {
    var he = 0, ac = 0, ro = 0, pe = 0, temp = 0;
    var cont = 2.47105381467;
    ac = 0;
    ro = 0;
    pe = pershes;

    ac = parseFloat(ac);
    ro = parseFloat(ro);
    pe = parseFloat(pe);

    ro = parseFloat(ro + pe / 40);
    ac = parseFloat(ac + ro / 4);
    he = parseFloat(ac / cont).toFixed(4);
    if (he == 0.0000) {
        he = 0;
    }
    return he;
}

function loadAlienation() {
    if ($('select#alienationState').attr("value") == 2) {
        $('select#alienationType').attr('disabled', false)
    } else {
        ;
        $('select#alienationType').val(0);
        $('select#alienationType').attr('disabled', true);
    }
}

function generateReport() {
    var id;
    var mode;
    var type = "report";
    var alienationStateId = $('select#alienationState').attr("value");
    var alienationMethodId = $('select#alienationType').attr("value");
    var geoTypeId = $('select#geologicalTypeId').attr("value");

    showNextButton(true);
    if (resetPage == true) {
        page = 1;
    } else {
        resetPage = true;
    }
    if (page > 1) {
        showPrevButton(true);
    } else {
        showPrevButton(false);
    }

    if ($('select#gnDivisionId').attr("value") > 0) {
        id = $('select#gnDivisionId').attr('value');
        mode = "byGNDivision";
    } else if ($('select#dsDivisionId').attr("value") > 0) {
        id = $('select#dsDivisionId').attr("value");
        mode = "byDSDivision";
    } else if ($('select#districtId').attr("value") > 0) {
        id = $('select#districtId').attr("value");
        mode = "byDistrict";
    } else {
        id = $('select#provinceId').attr("value");
        mode = "byProvince";
    }
    $.getJSON('${pageContext.request.contextPath}/common/LandLookupService', {id: id, mode: mode, type: type, alienation: alienationStateId, alienationMethod: alienationMethodId, geoType: geoTypeId, pageNo: page}, function (data, status) {
        //alert(status);
        loadLandList(data.landList);
    });
}

$(function () {
    $('select#provinceId').bind('change', function (evt1) {
        var id = $("select#provinceId").attr("value");
        $.getJSON('/eLand/common/DivisionLookupService', {id: id, mode: 1},
                function (data) {
                    var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                    var ds = data.districtsList;
                    for (var i = 0; i < ds.length; i++) {
                        options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                    }
                    $("select#districtId").html(options1);
                    var options2 = '<option value="0">' + $('#all').val() + '</option> ';
                    $("select#dsDivisionId").html(options2);
                    $("select#gnDivisionId").html(options2);
                });
    });

});

$(function () {
    $('select#districtId').bind('change', function (evt1) {
        var id = $("select#districtId").attr("value");
        $.getJSON('/eLand/common/DivisionLookupService', {id: id, mode: 2},
                function (data) {
                    var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                    var ds = data.dsDivisionList;
                    for (var i = 0; i < ds.length; i++) {
                        options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                    }
                    $("select#dsDivisionId").html(options1);
                    var options2 = '<option value="0">' + $('#all').val() + '</option> ';
                    $("select#gnDivisionId").html(options2);
                });
    });
});

$(function () {
    $('select#dsDivisionId').bind('change', function (evt1) {
        var id = $("select#dsDivisionId").attr("value");
        $.getJSON('/eLand/common/DivisionLookupService', {id: id, mode: 3},
                function (data) {
                    var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                    var ds = data.gnDivisionList;
                    for (var i = 0; i < ds.length; i++) {
                        options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                    }
                    $("select#gnDivisionId").html(options1);
                });
    });
});

function next() {
    previousPage = page + 1;
    page++;
    resetPage = false;
    generateReport();
}

function previous() {
    if (page <= 1) {
        page = 1;

    } else {
        page--;
    }
    resetPage = false;
    generateReport();
}

function showPrevButton(state) {
    if (state == true)
        $('#previousId').show();
    else
        $('#previousId').hide();
}

function showNextButton(state) {
    if (state == true)
        $('#nextId').show();
    else
        $('#nextId').hide();
}


</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="reportModule.do">
        <span><img src="${pageContext.request.contextPath}/images/print.png" alt="Print">
            <s:label value="%{getText('reports.label')}"/></span>
    </s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="detailLandsReport.do">
        <span><img src="${pageContext.request.contextPath}/images/list.png" alt="Detailed All Land Report">
            <s:label value="%{getText('detailed_lands_report.label')}"/></span>
    </s:a>
    <div id="back-button" align="right">
                <s:form><s:submit value="%{getText('back_btn.label')}" action="reportModule"/></s:form>
    </div>
</div>

<s:include value="../printCaller.jsp"/>
<div id="printDivId">

    <div id="init-land-report-list">
        <div class="page-title center-align">
            <h2><s:label value="%{getText('detailed_lands_report.label')}"/></h2>
        </div>
        <table class="width-635-px margin-10-auto" id="filter-lists">
            <tr>
                <td><s:label value="%{getText('province.label')}"/></td>
                <td><s:label value="%{getText('district.label')}"/></td>
            </tr>
            <tr>
                <td class="center-align">
                    <s:if test="%{provinceList.size==1}">
                        <s:select id="provinceId" name="provinceId" list="provinceList" cssClass="width-250-px"/>
                    </s:if>
                    <s:else>
                        <s:select id="provinceId" name="provinceId" list="provinceList" headerKey="0"
                                  headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
                    </s:else>
                </td>
                <td class="center-align">
                    <s:if test="%{districtList.size==1}">
                        <s:select id="districtId" name="districtName" list="districtList" cssClass="width-250-px"/>
                    </s:if>
                    <s:else>
                        <s:select id="districtId" name="districtName" list="districtList" headerKey="0"
                                  headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
                    </s:else>
                </td>
            </tr>
            <tr>
                <td><s:label value="%{getText('ds_division.label')}"/></td>
                <td><s:label value="%{getText('gn_division.label')}"/></td>
            </tr>
            <tr>
                <td class="center-align">
                    <s:if test="%{dsDivisionList.size==1}">
                        <s:select id="dsDivisionId" name="dsDivisionId" list="dsDivisionList" cssClass="width-250-px"/>
                    </s:if>
                    <s:else>
                        <s:select id="dsDivisionId" name="dsDivisionId" list="dsDivisionList" headerKey="0"
                                  headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
                    </s:else>
                </td>
                <td class="center-align">
                    <s:select id="gnDivisionId" name="gnDivisionId" list="gnDivisionList" headerKey="0"
                              headerValue="%{getText('all.label')}" cssClass="width-250-px"/></td>
            </tr>
            <tr>
                <td><s:label value="%{getText('alienation_state.label')}"/></td>
                <td><s:label value="%{getText('alienation_method.label')}"/></td>

            </tr>
            <tr>
                <td><s:select id="alienationState" name="alienationState"
                              list="alienationStatesList" headerKey="0"
                              headerValue="%{getText('select.label')}"
                              onChange="loadAlienation()" cssClass="width-250-px"/>

                </td>
                <td><s:select list="alienationTypeList" name="alienationType"
                              id="alienationType"
                              headerKey="0"
                              disabled="true"
                              headerValue="%{getText('all.label')}"
                              cssClass="width-250-px"/></td>

            </tr>
            <tr>
                <td colspan="2"><s:label value="%{getText('geological_type.label')}"/></td>
            </tr>
            <tr>
                <td><s:select id="geologicalTypeId" name="land.geologicalType.id"
                              list="geologicalTypeList" headerKey="0"
                              headerValue="%{getText('select.label')}" cssClass="width-250-px"/></td>
                <td><s:submit onclick="generateReport()" id="gen-report"
                              value="%{getText('gen_report.label')}"/></td>
            </tr>
        </table>
        <div id="report-header">
            <table class="width-100 margin-10-auto">
                <tr>
                    <td><s:label value="%{getText('district.label')}"/> : <s:textfield id="district"
                                                                                       cssStyle="border:none; background: none; color: #000;"
                                                                                       disabled="true"/></td>
                </tr>
                <tr>
                    <td><s:label value="%{getText('ds_division.label')}"/> : <s:textfield id="ds-name"
                                                                                          cssStyle="border:none; background: none; color: #000;"
                                                                                          disabled="true"/></td>
                </tr>
                <tr>
                    <td><s:label value="%{getText('gn_division.label')}"/> : <s:textfield id="gn-name"
                                                                                          cssStyle="border:none; background: none; color: #000;"
                                                                                          disabled="true"/></td>
                </tr>
            </table>
        </div>


        <div id="land-list">
            <%--<div id="printbtn" class="form-button-div right-align">
                &lt;%&ndash;<input type="button" value="Print" id="print"  class="print-btn">&ndash;%&gt;
                <input class="print-btn" type="button" value="Print" onclick="printInitialPage();">
            </div>--%>

            <table class="width-100 margin-10-auto table land-table" id="land-report" border="1" cellpadding="0"
                   cellspacing="0">

            </table>
            <div id="nextPreId" align="right">
                <img onclick="previous();" id="previousId" src="${pageContext.request.contextPath}/images/previous.gif"
                     border="none"/>
                <img onclick="next();" id="nextId" src="${pageContext.request.contextPath}/images/next.gif"
                     border="none"/>
            </div>

            <%--<div class="right-align print-div">
                <input class="print-btn" type="button"/>
            </div>--%>
        </div>

    </div>
</div>

<%-- Localization text for java script generated text --%>
<s:hidden id="ref" value="%{getText('reference_no.label')}"/>
<s:hidden id="dsDiv" value="%{getText('ds_division.label')}"/>
<s:hidden id="gnDiv" value="%{getText('gn_division.label')}"/>
<s:hidden id="lname" value="%{getText('name_of_the_land.label')}"/>
<s:hidden id="plan" value="%{getText('plan_no.label')}"/>
<s:hidden id="lot" value="%{getText('lot_no.label')}"/>
<s:hidden id="ext_pec" value="%{getText('extent_perches.label')}"/>
<s:hidden id="ext_hec" value="%{getText('extent_hectare.label')}"/>
<s:hidden id="all" value="%{getText('all.label')}"/>

<%-- Localization text for data tables --%>
<s:hidden id="display" value="%{getText('display.label')}"/>
<s:hidden id="records" value="%{getText('records.label')}"/>
<s:hidden id="search" value="%{getText('search.label')}"/>
<s:hidden id="no-record" value="%{getText('no_record.label')}"/>
<s:hidden id="showing" value="%{getText('showing.label')}"/>
<s:hidden id="to" value="%{getText('to.label')}"/>
<s:hidden id="of" value="%{getText('of.label')}"/>
<s:hidden id="entries" value="%{getText('entries.label')}"/>
<s:hidden id="first" value="%{getText('first.label')}"/>
<s:hidden id="last" value="%{getText('last.label')}"/>
<s:hidden id="next" value="%{getText('next.label')}"/>
<s:hidden id="previous" value="%{getText('previous.label')}"/>

<%-- Localization for report by GN Division--%>
<s:hidden id="report-title" value="%{getText('land_list_by_gn_division.label')}"/>
<s:hidden id="print" value="%{getText('print.label')}"/> 